package com.example.wu5hen.n0te;

import android.app.Dialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.LoaderManager;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;

import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;

import com.example.wu5hen.n0te.Fields.DiaryColumns;
import com.example.wu5hen.n0te.Adapter.OnRecyclerViewListener;
import com.example.wu5hen.n0te.DiaryProvider;
import com.example.wu5hen.n0te.Diary;

import static com.example.wu5hen.n0te.Fields.AUTHORITY;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private FloatingActionButton fab;
    private Adapter adapter;
    //private Cursor c;


    private static final String[] PROJECTION = new String[] { DiaryColumns._ID, DiaryColumns.TITLE, DiaryColumns.BODY,DiaryColumns.CREATED };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);

        initViews();
        //initLoader();
        if (fab != null) {
            //
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showDialog();
                    //showEdit();
                }
            });
        }
    }
    
    private void initViews() {

        fab = (FloatingActionButton) findViewById(R.id.fab);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        if (adapter == null) {
            //try one try
            Cursor c = getContentResolver().query(Fields.DiaryColumns.CONTENT_URI, null, null, null, DiaryColumns.DEFAULT_SORT_ORDER);
            adapter = new Adapter(this, c, 1);

            //int itemcount=adapter.getItemCount();

            adapter.setOnRecyclerViewListener(new Adapter.OnRecyclerViewListener(){
                @Override
                public void onItemClick(View view,int position){
                    Log.i("ccc", "onItemClick: "+position);
                    //showDialog();
                    updatedairy(position);
                }
                @Override
                public void onItemLongClick(View view,int position){
                    Log.i("lll", "onItemLongClick: "+position);
                    Log.i("1", "getTag="+view.getTag());
                    int count=adapter.getItemCount();
                    long aid=adapter.getItemId(position);
                    Log.e("##########", "aid= "+aid );
                    Uri uri=Uri.parse("content://" + AUTHORITY + "/diaries/"+(count-position));
                    getContentResolver().delete(uri,null,null);

                    Intent intent = new Intent(MainActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            });
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);



    }
    private void showDialog() {
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        View v = getLayoutInflater().inflate(R.layout.diary_edit, null);
        dialog.setView(v);
        dialog.setCancelable(false);
        dialog.show();

        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 1000;
        params.height = 1400 ;
        dialog.getWindow().setAttributes(params);

        final EditText mtitle=(EditText) v.findViewById(R.id.title);
        final EditText mbody=(EditText) v.findViewById(R.id.body);

        Button cancel=(Button) v.findViewById(R.id.cancel);
        Button confirm=(Button) v.findViewById(R.id.confirm);

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String title = mtitle.getText().toString();
                String body = mbody.getText().toString();

                ContentValues values = new ContentValues();
                values.put(Fields.DiaryColumns.CREATED, DiaryProvider
                        .getFormateCreatedDate());
                values.put(DiaryColumns.TITLE, title);
                values.put(DiaryColumns.BODY, body);
                Uri uri;
                uri=getContentResolver().insert(DiaryColumns.CONTENT_URI, values);
                Log.i("uri", "new"+uri);
                dialog.dismiss();
                Intent intent = new Intent(MainActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

    }
    private void updatedairy(int position){
        final AlertDialog dialog = new AlertDialog.Builder(this).create();
        View v = getLayoutInflater().inflate(R.layout.diary_edit, null);
        dialog.setView(v);
        dialog.setCancelable(false);
        dialog.show();

        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 1000;
        params.height = 1400 ;
        dialog.getWindow().setAttributes(params);

        final EditText mtitle=(EditText) v.findViewById(R.id.title);
        final EditText mbody=(EditText) v.findViewById(R.id.body);

        Button cancel=(Button) v.findViewById(R.id.cancel);
        Button confirm=(Button) v.findViewById(R.id.confirm);

        final Uri mUri;
        Cursor mCursor;
        int count=adapter.getItemCount();
        long aid=adapter.getItemId(position);
        Log.e("##########", "aid= "+aid );
        mUri=Uri.parse("content://" + AUTHORITY + "/diaries/"+aid);
        ContentResolver contentResolver=this.getContentResolver();
        mCursor = getContentResolver().query(mUri, PROJECTION, null, null, null);
        mCursor.moveToFirst();
        String title = mCursor.getString(1);
        mtitle.setTextKeepState(title);
        String body = mCursor.getString(2);
        mbody.setTextKeepState(body);

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String title = mtitle.getText().toString();
                String body = mbody.getText().toString();

                ContentValues values = new ContentValues();
                values.put(Fields.DiaryColumns.CREATED, DiaryProvider
                        .getFormateCreatedDate());
                values.put(DiaryColumns.TITLE, title);
                values.put(DiaryColumns.BODY, body);
                getContentResolver().update(mUri, values,null,null);
                dialog.dismiss();
                Intent intent = new Intent(MainActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }
}
